<template>
    <div>
      <h2>添加员工</h2>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item to="/AdminMain/AdminHome">主页</el-breadcrumb-item>
        <el-breadcrumb-item to="/AdminMain/EmployeeManager">员工管理</el-breadcrumb-item>
        <el-breadcrumb-item>添加员工</el-breadcrumb-item>
      </el-breadcrumb>
  
      <el-card style="margin-top: 20px;">
        <el-form :model="form" :rules="rules" ref="addForm" label-width="100px">
          <el-form-item label="工号">
            <el-input placeholder="工号由系统自动生成" disabled />
          </el-form-item>
  
          <el-form-item label="姓名" prop="employeeName">
            <el-input v-model="form.employeeName" placeholder="姓名" />
          </el-form-item>
  
          <el-form-item label="性别" prop="employeeGender">
            <el-input v-model="form.employeeGender" placeholder="性别" />
          </el-form-item>
  
          <el-form-item label="年龄" prop="employeeAge">
            <el-input v-model="form.employeeAge" placeholder="年龄" />
          </el-form-item>
  
          <el-form-item label="职务" prop="staff">
            <el-input v-model="form.staff" placeholder="职务" />
          </el-form-item>
  
          <el-form-item label="备注信息">
            <el-input v-model="form.employeeMessage" placeholder="备注信息" />
          </el-form-item>
  
          <el-form-item>
            <el-button type="success" @click="onSubmit">添加</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </template>
  
  <script setup>
  import { ref } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import axios from 'axios'
  
  const router = useRouter()
  
  // 表单数据
  const form = ref({
    employeeName: '',
    employeeGender: '',
    employeeAge: '',
    staff: '',
    employeeMessage: ''
  })
  
  // 表单校验规则
  const rules = {
    employeeName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
    employeeGender: [{ required: true, message: '请输入性别', trigger: 'blur' }],
    employeeAge: [{ required: true, message: '请输入年龄', trigger: 'blur' }],
    staff: [{ required: true, message: '请输入职务', trigger: 'blur' }]
  }
  
  const addForm = ref(null)
  
  const onSubmit = () => {
    addForm.value.validate((valid) => {
      if (valid) {
        axios.post('/server/employee/add', form.value)
          .then(res => {
            if (res.data.code === 200) {
              ElMessage.success('添加成功')
              router.push('/AdminMain/EmployeeManager')
            } else {
              ElMessage.error(res.data.message || '添加失败')
            }
          })
          .catch(() => {
            ElMessage.error('请求失败')
          })
      } else {
        ElMessage.warning('请填写必填项')
      }
    })
  }
  </script>
  
  